﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BusinessLayer.Model.Services.Abstract;
using BusinessLayer.Model.Services.Concrete;
using BusinessLayer.Model.Entities;
using DataAccess.Services;

namespace intecnologia.Admin
{
    public partial class Parameters : System.Web.UI.Page
    {
        ISessionService _sessionService = new SessionService();
        ITwitterService _twitterService = new TwitterService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (_sessionService.SessionExpired())
                Response.Redirect("~/Default.aspx");

            if (!IsPostBack)
            {
                LoadTwitterDetails();
            }
        }

        protected void btnChangeUsername_Click(object sender, EventArgs e)
        {
            ToggleChangeUsernameMode();
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            ToggleChangePasswordMode();
        }

        private void LoadTwitterDetails()
        {
            TwitterAccount account = _twitterService.GetAccount();

            lblError.Visible = false;

            btnChangeUsername.Text = "Cambiar";
            btnChangePassword.Text = "Cambiar";

            lblTwitterUsername.Visible = true;
            lblTwitterUsername.Text = account.Username;
        }

        private void ToggleChangeUsernameMode()
        {
            if (btnChangeUsername.Text == "Cambiar")
            {
                tbTwitterUserName.Visible = true;
                lblTwitterUsername.Visible = false;
                btnChangeUsername.Text = "Aceptar";
            }
            else if (btnChangeUsername.Text == "Aceptar")
            {
                if (tbTwitterUserName.Text != String.Empty)
                {
                    _twitterService.ChangeAccount(tbTwitterUserName.Text);

                    tbTwitterUserName.Visible = false;
                    LoadTwitterDetails();
                }
                else
                    DisplayError("Debe introducir el nombre de usuario.");
            }
        }

        private void ToggleChangePasswordMode()
        {
            if (btnChangePassword.Text == "Cambiar")
            {
                tbTwitterPassword.Visible = true;
                btnChangePassword.Text = "Aceptar";
            }
            else if (btnChangePassword.Text == "Aceptar")
            {
                if (tbTwitterPassword.Text != String.Empty)
                {
                    _twitterService.ChangePassword(tbTwitterPassword.Text);

                    tbTwitterPassword.Visible = false;
                    LoadTwitterDetails();
                }
                else
                    DisplayError("Debe introducir la clave.");
            }
        }

        private void DisplayError(string errorMsg)
        {
            lblError.Text = errorMsg;
            lblError.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Default.aspx");
        }
    }
}
